package com.acs.player;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.acs.player.constants.PlayerConst;
import com.acs.player.entities.Music;
import com.acs.player.media.Player;
import com.acs.player.media.RetrieveData;
import com.acs.player.views.simple.TrackAdapter;

public class CoverRowActivity extends Activity {
	
	private Menu menu;
	private List<Music> albumTrackList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cover_view_row);  
        
        Intent intent = getIntent();
        List<Integer> trackIndexList = intent.getIntegerArrayListExtra(PlayerConst.COVER_TRACK_INDEX_KEY);
        
        LinearLayout layout = (LinearLayout) findViewById(R.id.player_body);
        layout.removeAllViews();      
        
        albumTrackList = getAlbumTrackList(trackIndexList);
        
        //CREATE TRACK LIST 
		TrackAdapter trackAdapter = new TrackAdapter(this, R.layout.row_music, albumTrackList);	
		ListView listView = new ListView(this);
    	listView.setAdapter(trackAdapter);
        
        layout.addView(listView); 
        
        //CREATE MENU
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.the_mother_of_all_views_navigator);       
        menu = new Menu();
        LinearLayout linearLayout = menu.createMenu(this);
        relativeLayout.addView(linearLayout);
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
    	
    }
    
    public void click(View view){
    	menu.click(view);
    }
    
    public List<Music> getAlbumTrackList(List<Integer> trackIndexList){  	
        RetrieveData retrieveData = RetrieveData.getRetrieveData(this);
        List<Music> allTracksList = retrieveData.getAllTracksList();
    	
    	List<Music> albumTrackList = new ArrayList<Music>();
    	for (Integer index : trackIndexList) {
			Music music = allTracksList.get(index);
			albumTrackList.add(music);
		}
    	
    	return albumTrackList;
    }
    
	public void goToPlayerActivity(int trackIndex){	
		Player player = Player.getPlayer();
		
		if(!player.getTrackListsToPlay().equals(albumTrackList)){
			player.setTrackListsToPlay(albumTrackList);
		}
		
    	Intent intent = new Intent(CoverRowActivity.this, PlayerActivity.class); 
    	intent.putExtra(PlayerConst.CURRENT_TRACK_INDEX_KEY, trackIndex);
    	intent.putExtra(PlayerConst.PREVIOUS_ACTIVITY_KEY, PlayerConst.COVER_ROW_VIEW);
    	
    	startActivity(intent);
	}
}
